<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Sign');
Globals::requireTable('Vmoney');

class SignController extends Controller
{
	protected $UserTable;
	protected $SignTable;
	protected $VmoneyTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable          = new UserTable($config);
		$this->SignTable          = new SignTable($config);
		$this->VmoneyTable        = new VmoneyTable($config);
	}
	
	/**
	 * 签到
	 * uid/token&machine_type
	 */
	public function indexAction()
	{
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'vmoney'=>'','days'=>'','allmoney'=>'','extra_vmoney'=>''));
	        exit();
	    }
	    
	    $sign_time = strtotime(date('Y-m-d'));
	    //$sign_time = strtotime('2017-03-15');
	    
	    $day0 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>$sign_time));
	    if($day0['id'] > 1){
	        echo json_encode(array('returncode'=>-2,'vmoney'=>'','days'=>'','allmoney'=>'','extra_vmoney'=>''));
	        exit();
	    }
	    
	    $day4 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>($sign_time-86400*4)));
	    $day3 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>($sign_time-86400*3)));
	    $day2 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>($sign_time-86400*2)));
	    $day1 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>($sign_time-86400*1)));
	    
	    if($day1['v_money'] == '0.05' && $day2['v_money'] == '0.05' && $day3['v_money'] == '0.05' && $day4['v_money'] == '0.05'){
	        $sign_data['uid'] = $uid;
	        $sign_data['v_money'] = 0.3;
	        $sign_data['day'] = $sign_time;
	        $this->SignTable->add($sign_data);
	         
	        $user_data = $this->UserTable->getRow($uid);
	        $user_data['v_money'] += 0.3;
	        $this->UserTable->update($user_data,$uid);
	        
	        $vmoney_data['uid'] = $uid;
	        $vmoney_data['money'] = 0.3;
	        $vmoney_data['type'] = 2;
	        $vmoney_data['remark'] = '用户签到获得0.3V币';
	        $this->VmoneyTable->insert($vmoney_data);
	        
	        $vcount = 0;
	        $listvalue = $this->VmoneyTable->listAll(array('uid'=>$uid));
	        foreach ($listvalue as $k=>$v){
	            $vcount += $v['money'];
	        }
	        
	        echo json_encode(array('returncode'=>1,'vmoney'=>'0.05','days'=>'5','allmoney'=>(string)$vcount,'extra_vmoney'=>'0.25'));
	        exit();
	    }else{
	        $sign_data['uid'] = $uid;
	        $sign_data['v_money'] = 0.05;
	        $sign_data['day'] = $sign_time;
	        $this->SignTable->add($sign_data);
	         
	        $user_data = $this->UserTable->getRow($uid);
	        $user_data['v_money'] += 0.05;
	        $this->UserTable->update($user_data,$uid);
	        
	        $vmoney_data['uid'] = $uid;
	        $vmoney_data['money'] = 0.05;
	        $vmoney_data['type'] = 2;
	        $vmoney_data['remark'] = '用户签到获得0.05V币';
	        $this->VmoneyTable->insert($vmoney_data);
	        
	        $day_count = 1;
	        if($day1['v_money'] == '0.05'){
	            $day_count++;
	            if($day2['v_money'] == '0.05'){
	                $day_count++;
	                if($day3['v_money'] == '0.05'){
	                    $day_count++;
	                }
	            }
	        }
	        
	        $vcount = 0;
	        $listvalue = $this->VmoneyTable->listAll(array('uid'=>$uid));
	        foreach ($listvalue as $k=>$v){
	            $vcount += $v['money'];
	        }
	        
	        echo json_encode(array('returncode'=>1,'vmoney'=>'0.05','days'=>(string)$day_count,'allmoney'=>(string)$vcount,'extra_vmoney'=>'0'));
	        exit();
	    }
	    
	    
	}
	
	public function issignAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'allmoney'=>'','days'=>'0','issign'=>0));
	        exit();
	    }
	    
	    $sign_time = strtotime(date('Y-m-d'));
	    //$sign_time = strtotime('2017-03-15');
	    
	    $user_data = $this->UserTable->getRow($uid);
	    
	    $day0 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>$sign_time));
	    
	    $day4 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>($sign_time-86400*4)));
	    $day3 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>($sign_time-86400*3)));
	    $day2 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>($sign_time-86400*2)));
	    $day1 = $this->SignTable->getRow(array('uid'=>$uid,'day'=>($sign_time-86400*1)));
	    
	    $vcount = 0;
	    $listvalue = $this->VmoneyTable->listAll(array('uid'=>$uid));
	    foreach ($listvalue as $k=>$v){
	        $vcount += $v['money'];
	    }
	    
	    if($day0['id'] > 1){
	        $day_count = 1;
	        if($day1['v_money'] == '0.05'){
	            $day_count++;
	            if($day2['v_money'] == '0.05'){
	                $day_count++;
	                if($day3['v_money'] == '0.05'){
	                    $day_count++;
	                    if($day4['v_money'] == '0.05'){
	                        $day_count++;
	                    }
	                }
	            }
	        }
	        
	        echo json_encode(array('returncode'=>0,'allmoney'=>$vcount,'days'=>(string)$day_count,'issign'=>1));
	        exit();
	    }else{
	        echo json_encode(array('returncode'=>0,'allmoney'=>$vcount,'days'=>'0','issign'=>0));
	        exit();
	    }
	}
	
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('SignController', 'Controller');
